package com.SFA.Main;


import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

import com.FrameWork.Common.MyApplication;
import com.FrameWork.Entity.FunctionEntity;
import com.SFA.R;


public class MainButtonClickListener implements
OnClickListener,
OnTouchListener
{
	public void Click(Tab tab)
	{
		View view = new View(null);
		view.setTag(tab.getTag());
		onClick(view);
	}

	// 点击按钮操作
	public void onClick(final View v)
	{

		FunctionEntity functionEntity = (FunctionEntity) v.getTag();

		Intent intent = null;
		Class c = null;
		if (functionEntity.CODE.equals("MESSAGE"))
		{
			try
			{
				c = Class.forName("com.SFA.Message.MessageListActivity");
			}
			catch (ClassNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (functionEntity.CODE.equals("SYNC"))
			{
				try
				{
					c = Class.forName("com.FrameWork.Synchoronization.SyncActivity");
				}
				catch (ClassNotFoundException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if (functionEntity.CODE.equals("ORDERMANAGEMENT"))
			{
				try
				{
					c = Class.forName("com.SFA.OrderManagement.OrderListActivity");
				}
				catch (ClassNotFoundException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if (functionEntity.CODE.equals("EXIT"))
			{
				try
				{
					new AlertDialog.Builder(v.getContext()).setIcon(android.R.drawable.ic_dialog_alert)
					.setTitle(R.string.MSG_002)
					.setPositiveButton(R.string.EXIT,
							new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog,
								int which)
						{
							((MyApplication)v.getContext().getApplicationContext()).exit();
						
						}
					})
					.setNegativeButton(R.string.CANCEL,
							null)
							.show();
				}
				catch (Exception e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return;
			}
			else
				if (functionEntity.CODE.equals("ABOUT"))
				{
					final Dialog dialog = new Dialog(v.getContext());
					dialog.setContentView(R.layout.about);
					dialog.getWindow()
					.setWindowAnimations(R.style.dialogWindowAnim);
					dialog.show();

					TextView textView = (TextView) dialog.getWindow()
							.findViewById(R.id.about_version);
					PackageManager packageManager = v.getContext()
							.getPackageManager();
					PackageInfo packageInfo = null;
					try
					{
						packageInfo = packageManager.getPackageInfo(((Activity) v.getContext()).getPackageName(),
								0);
					}
					catch (NameNotFoundException e)
					{
						e.printStackTrace();
					}
					textView.setText("本" + packageInfo.versionName);
					dialog.getWindow()
					.findViewById(R.id.about_but_back)
					.setOnClickListener(new OnClickListener()
					{
						public void onClick(View v)
						{
							dialog.cancel();
						}
					});
					return;

				}
				else
					if (functionEntity.TYPE.equals("1"))
					{
						try
						{
							c = Class.forName("cofco.Mobile.UI.NewsListActivity");
						}
						catch (ClassNotFoundException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
						if (functionEntity.TYPE.equals("4"))
						{
							try
							{
								c = Class.forName("com.SFA.Web.WebViewActivity");
							}
							catch (ClassNotFoundException e)
							{
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

		if (c != null)
		{
			intent = new Intent(((Activity) v.getContext()), c);
			intent.putExtra("title", functionEntity.Title);// 标题
			intent.putExtra("RSS", functionEntity.RSS);// 获取RSSURL
			intent.putExtra("URL", functionEntity.URL);
			//intent.putExtra("URL", "http://27.115.112.50:888/SFAservicev3/route.aspx");
			intent.putExtra("CODE", functionEntity.CODE);
			((Activity) v.getContext()).startActivity(intent);
			((Activity)v.getContext()).overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
		}


	}

	public boolean onTouch(View v, MotionEvent event)
	{
		if (event.getAction() != MotionEvent.ACTION_MOVE)
		{
			v.setBackgroundResource(0);
		}

		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			v.setBackgroundResource(android.R.color.darker_gray);
		}
		else
			if (event.getAction() == MotionEvent.ACTION_UP)
			{
				// 改为抬起时的图片
				v.setBackgroundResource(0);
			}

		return false;
	}

}
